// ==================================================================
// 描 述：
// 作 者：王征:D
// 创建时间：2019/06/01 19:42:34
// ==============================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using Yunchang;

namespace AutoChess
{
    public class AutoChessEventPanel : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler, IPointerClickHandler
    {
        public bool RaycastSceneUI;
        public void OnPointerDown(PointerEventData eventData)
        {
            AutoChessFieldViewCtr.Ins.InputCtr.OnPointerDown(eventData);
        }

        public void OnPointerUp(PointerEventData eventData)
        {
            AutoChessFieldViewCtr.Ins.InputCtr.OnPointerUp(eventData);
        }

        public void OnDrag(PointerEventData eventData)
        {
            AutoChessFieldViewCtr.Ins.InputCtr.OnDrag(eventData);
        }

        public void OnPointerClick(PointerEventData eventData)
        {
            EventManager.TriggerEvent(CommonEvent.ON_CLICK_BACK_PANEL);
            if (!RaycastSceneUI)
                return;

            PassEvent(eventData, ExecuteEvents.pointerClickHandler);
        }

        public void PassEvent<T>(PointerEventData data, ExecuteEvents.EventFunction<T> function)
        where T : IEventSystemHandler
        {
            List<RaycastResult> results = new List<RaycastResult>();
            EventSystem.current.RaycastAll(data, results);
            GameObject current = data.pointerCurrentRaycast.gameObject;
            for (int i = 0; i < results.Count; i++)
            {
                if (current != results[i].gameObject)
                {
                    var sceneUI = results[i].gameObject.transform.parent.parent.parent.GetComponent<AutoChessHeroInfoUI>();
                    if (sceneUI != null)
                    {
                        ExecuteEvents.Execute(results[i].gameObject, data, function);
                        break;
                    }
                }
            }
        }
    }
}
